<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>v-for 实例</title>
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <script type="text/javascript" src="../assets/js/vue.js"></script>
</head>
<body>
    <h1>v-for实例</h1>
    <hr>
    <div id="app">
        <ul>
            <li v-for="item in sortItems">
                {{item}}
            </li>
        </ul>
        <hr>
        <ul>
            <li v-for="(student,index) in sortStudents">
                 {{index+1}}:{{student.name}}-{{student.age}}
            </li>
        </ul>
    </div>
    <script type="text/javascript">
        var app=new Vue({
            el:'#app',
            data:{
                items:[53,23,14,54,36,2,75],
                students:[
                    {name:'Chen',age:23},
                    {name:'Li',age:21},
                    {name:'Panda',age:13},
                    {name:'King',age:33},
                     ]
            },
           
            computed:{
               sortItems:function(){
                   return this.items.sort(sortNumber)
               } ,
               sortStudents:function(){
                   return sortByKey(this.students,'age')
               }
            }
        });
        function sortNumber(a,b){
            return a-b
        }
        //数组对象排序
        function sortByKey(array,key){
            return array.sort(function(a,b){
                var x=a[key];
                var y=b[key];
                return ((x<y)?-1:((x>y)?1:0));
            })
        }
    </script>
</body>
</html>